<template>
  <div class="tktk_container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="ruleForm.name" class="w450"></el-input>
      </el-form-item>

      <el-form-item label="活动描述" prop="descName">
        <el-input v-model="ruleForm.descName" class="w450" :maxlength="30" show-word-limit></el-input>
      </el-form-item>

      <el-form-item label="活动商品" prop="infoList">
        <div class="mb10">
          <el-button type="primary" @click="selects" size="small" icon="el-icon-plus">选择商品</el-button>
        </div>
        <div class="goods_list">
          <el-tag v-for="item in ruleForm.infoList" :key="item.id" closable @close="deleteTag(item)">
            {{ item.name }}
          </el-tag>
        </div>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <!--选择商品-->
    <el-drawer title="选择商品" :visible.sync="drawerBox" :wrapperClosable="false" :direction="direction"
      :append-to-body="true" :before-close="cancelBox" size="80%">
      <select-goods-multiple @transfer="transfer" :list="ruleForm.infoList"></select-goods-multiple>
    </el-drawer>
  </div>
</template>

<script>
import { getRecommendDetail, updateRecommendDetail } from '@/api/tktk/goods'
import selectGoodsMultiple from '@/components/SelectGoods/multipleList'
import { EventBus } from '@/utils/event-bus.js'
export default {
  components: {
    selectGoodsMultiple
  },
  data() {
    return {
      ruleForm: {
        id: '',
        name: '精品推荐',
        descName: '为你推荐优质好货',
        infoList: []
      },
      drawerBox: false,
      direction: 'rtl',
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
        ],
        descName: [
          { required: true, message: '请输入活动描述', trigger: 'blur' },
        ],
        infoList: [
          { required: true, message: '请选择商品' },
        ]
      }
    }
  },

  methods: {
    selects() {
      this.drawerBox = true
    },

    cancelBox() {
      this.drawerBox = false
    },

    deleteTag(row) {
      const _this = this
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        _this.ruleForm.infoList = _this.ruleForm.infoList.filter(item => item.id !== row.id)
      })
    },

    transfer(data) {
      this.drawerBox = false
      this.ruleForm.infoList = data
    },

    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          updateRecommendDetail(this.ruleForm).then(res => {
            if (res.code === 200) {
              this.$message.success('保存成功')
              this.getRecommendInfo()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    resetForm(ruleForm) {
      this.$refs[ruleForm].resetFields()
    },

    getRecommendInfo() {
      getRecommendDetail().then(res => {
        if (res.code === 200) {
          console.log(res.data)
          const { infoList, name, descName, id } = res.data
          this.ruleForm = {
            id,
            name,
            descName,
            infoList
          }
        }
      })
    }
  },

  mounted() {
    this.getRecommendInfo()
  }
}
</script>

<style lang="scss">
.el-tag {
  margin: 5px;
}
</style>
